package com.springbootweb.springbootweb.config;

import com.springbootweb.springbootweb.component.LoginHandleInterceptor;
import com.springbootweb.springbootweb.component.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;



// 使用webmvcconfigurerAdapter可以来扩展SpringMvc功能
// @EnableWebMvc // 所有自动配置都失效
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //WebMvcConfigurer.super.addViewControllers(registry);
        // 浏览器发送 来到success页面
        registry.addViewController("/chennan").setViewName("success");
    }
    @Bean // 注册为组件
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer adapter = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
                registry.addViewController("/main.html").setViewName("dashboard");
            }
            // 注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //WebMvcConfigurer.super.addInterceptors(registry);
                registry.addInterceptor(new LoginHandleInterceptor())
                        .addPathPatterns("/main.html","/emps","/emp","/emp/**");
            }
        };
        return adapter;
    }
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}
